package com.star.carservice.client.fragment;

import java.util.Timer;
import java.util.TimerTask;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.star.carservice.R;
import com.star.carservice.client.activity.RegisterActivity;
import com.star.carservice.client.fragment.RegisterStep1Fragment.SendValidCodeTask;
import com.star.carservice.client.fragment.base.BaseFragment;
import com.star.carservice.client.utils.StringUtils;
import com.star.carservice.client.utils.ToastUtils;
import com.star.carservice.client.widget.MyProgressDialog;
import com.star.carserviceapi.domain.request.SendValidCodeRequest;
import com.star.carserviceapi.domain.request.VerifyValidCodeRequest;
import com.star.carserviceapi.domain.result.SendValidCodeResult;
import com.star.carserviceapi.domain.result.VerifyValidCodeResult;

public class RegisterStep2Fragment extends BaseFragment {

	private final static String TAG = RegisterStep2Fragment.class
			.getSimpleName();

	MyProgressDialog mProgressDialog = null;

	@InjectView(R.id.btn_send_sms)
	Button btn_send_sms;

	@InjectView(R.id.input_code)
	EditText input_code;

	@Override
	public String getLogTag() {
		return TAG;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		mProgressDialog = new MyProgressDialog(getActivity(), false);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		if (CacheViewIsNull()) {
			mCacheView = inflater.inflate(R.layout.frag_register_step2,
					container, false);

			ButterKnife.inject(this, mCacheView);

			startTimer();
		}

		removeCacheView();

		return mCacheView;
	}

	@OnClick(R.id.btn_submit_code)
	public void submitCode(View view) {
		if (StringUtils.isEmpty(getCode())) {
			return;
		}
		
		execAsyncTask(new VerifyValidCodeTask(), new VerifyValidCodeRequest(
				getMobile(), getCode()));
	}

	@OnClick(R.id.btn_send_sms)
	public void onSendSms(View v) {
		if (StringUtils.isBlank(getMobile())) {
			return;
		}

		execAsyncTask(new SendValidCodeTask(), new SendValidCodeRequest(
				getMobile(), SendValidCodeRequest.ACTION_CREATE));
	}

	private String getCode() {
		return input_code.getText().toString().trim();
	}

	private String getMobile() {
		RegisterActivity parnet = (RegisterActivity) getActivity();

		return parnet.getMobile();
	}

	Timer timer = null;

	int time_max_s=60;
	
	int time = time_max_s;

	public void startTimer() {
		btn_send_sms.setEnabled(false);
		timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {

			@Override
			public void run() {

				btn_send_sms.post(new Runnable() {

					@Override
					public void run() {
						btn_send_sms.setText((time--) + "s");

						if (time <= 0) {
							timer.cancel();
							btn_send_sms.setText("重发");
							btn_send_sms.setEnabled(true);
							time = time_max_s;
						}
					}
				});

			}
		}, 1, 1000);

	}

	class SendValidCodeTask extends
			AsyncTask<SendValidCodeRequest, Void, SendValidCodeResult> {

		@Override
		protected void onPreExecute() {
			mProgressDialog.show();
		}

		@Override
		protected SendValidCodeResult doInBackground(
				SendValidCodeRequest... params) {
			return getApi().sendValidCodeRequest(params[0]);
		}

		@Override
		protected void onPostExecute(SendValidCodeResult result) {
			mProgressDialog.dismiss();

			if (result.successful()) {
				startTimer();
			}
		}
	}

	class VerifyValidCodeTask extends
			AsyncTask<VerifyValidCodeRequest, Void, VerifyValidCodeResult> {

		@Override
		protected void onPreExecute() {
			mProgressDialog.show();
		}
		
		@Override
		protected VerifyValidCodeResult doInBackground(
				VerifyValidCodeRequest... params) {
			
			return getApi().verifyValidCode(params[0]);
		}
		
		@Override
		protected void onPostExecute(VerifyValidCodeResult result) {
			mProgressDialog.dismiss();

			if (result.successful()) {
				
				getActivity().getSupportFragmentManager().beginTransaction()
				.replace(R.id.view_placeholder, new RegisterStep3Fragment())
				.addToBackStack(null)

				.commit();
			} else {
				ToastUtils.show(getActivity(), result.getMessage());
			}
		}

	}
}